﻿using System;
using Microsoft.Win32;

namespace DevExpress.DemoCenter {
    public static class InstallHelper {
        public static bool IsProductInstalled(string productKeyName) { return IsProductInstalled(productKeyName, RegistryViewer.Current); }
        public static bool IsXafInstalled() { return IsXafInstalled(RegistryViewer.Current); }
        public static bool IsProductInstalled(string productKeyName, RegistryViewerBase registryViewer) {
            bool softwareRecordExists = false;
            bool devExpressRecordExists = false;
            bool productRecordExists = false;
            foreach(string softwareKey in new string[] { "Software", "Software/Wow6432Node" }) {
                softwareRecordExists = softwareRecordExists || registryViewer.IsSubkeyExists(Registry.LocalMachine, softwareKey);
                if(!softwareRecordExists) continue;
                devExpressRecordExists = devExpressRecordExists || registryViewer.IsSubkeyExists(Registry.LocalMachine, softwareKey + "/DevExpress");
                if(!devExpressRecordExists) continue;
                productRecordExists = productRecordExists || registryViewer.IsSubkeyExists(Registry.LocalMachine, string.Format(softwareKey + "/DevExpress/Components/v{0}/{1}", AssemblyInfo.VersionShort, productKeyName));
            }
            if(!softwareRecordExists) return false;
            if(!devExpressRecordExists) return true;
            return productRecordExists;
        }
        public static bool IsXafInstalled(RegistryViewerBase registryViewer) {
            bool softwareRecordExists = false;
            bool devExpressRecordExists = false;
            bool productRecordExists = false;
            foreach(string softwareKey in new string[] { "Software", "Software/Wow6432Node" }) {
                softwareRecordExists = softwareRecordExists || registryViewer.IsSubkeyExists(Registry.LocalMachine, softwareKey);
                if(!softwareRecordExists) continue;
                devExpressRecordExists = devExpressRecordExists || registryViewer.IsSubkeyExists(Registry.LocalMachine, softwareKey + "/DevExpress");
                if(!devExpressRecordExists) continue;
                productRecordExists = productRecordExists || registryViewer.IsSubkeyExists(Registry.LocalMachine, string.Format(softwareKey + "/DevExpress/eXpressApp Framework/v{0}/eXpressAppFramework", AssemblyInfo.VersionShort));
            }
            if(!softwareRecordExists) return false;
            if(!devExpressRecordExists) return true;
            return productRecordExists;
        }
#if !WPF && !SL
        static bool? isWpfInstalled = null;
        static bool? isSilverlightInstalled = null;
        public static readonly string WpfProductsKeyName = "WPF Components";
        public static readonly string SilverlightProductsKeyName = "SL Components";
        public static bool IsWpfInstalled {
            get {
                if(isWpfInstalled == null) {
                    isWpfInstalled = IsProductInstalled(WpfProductsKeyName);
                }
                return (bool)isWpfInstalled;
            }
        }
        public static bool IsSilverlightInstalled {
            get {
                if(isSilverlightInstalled == null) {
                    isSilverlightInstalled = IsProductInstalled(SilverlightProductsKeyName);
                }
                return (bool)isSilverlightInstalled;
            }
        }
#endif
    }
    public abstract class RegistryViewerBase {
        public bool IsSubkeyExists(RegistryKey key, string subkey) {
            string[] pathParts = subkey.Replace('\\', '/').Split('/');
            object currentKey = key;
            foreach(string pathPart in pathParts) {
                currentKey = OpenSubKey(currentKey, pathPart);
                if(currentKey == null) return false;
            }
            return true;
        }
        public abstract object OpenSubKey(object key, string subkey);
    }
    public class RegistryViewer : RegistryViewerBase {
        public static RegistryViewer Current = new RegistryViewer();
        public override object OpenSubKey(object key, string subkey) {
            return ((RegistryKey)key).OpenSubKey(subkey);
        }
    }
}
